package hut.game.unit;

import hut.game.table.BanCo;
import hut.game.table.VanCo;
import android.widget.ImageView;

public class Sy extends QuanCo {
	public Sy() {
		super();
	}
	@Override
	public void KhoiTao(int phe, String name, String thutu, int stt,
			boolean khoa, int x, int y) {
		super.KhoiTao(phe, name, thutu, stt, khoa, x, y);
	}
	// Danh cho Computer
	private int diem  = 2;
	public int getDiem() {
		if (this.TrangThai != 0)
        {
//            if (this.Hang == 0 || this.Hang == 9)
//                diem += 50;
//            else if (this.Hang == 1 || this.Hang == 8)
//                diem += 52;
//            else if (this.Hang == 3 || this.Hang == 7)
//                diem += 39;
//            else 
			if(this.TrangThai == 1)
				diem = 30;
        }
        else
            diem = 0 ;
		return diem;
	}
	
	@Override
	public int KiemTra(int i, int j) {
		boolean turn = false;
		if(this.TrangThai == 2){
			if ((i >= 0 && i <= 2 && j >= 3 && j <= 5) || (i >= 7 && i <= 9 && j >= 3 && j <= 5))
	            if ((i == Hang + 1 && j == Cot + 1) || (i == Hang + 1 && j == Cot - 1) || (i == Hang - 1 && j == Cot - 1) || (i == Hang - 1 && j == Cot + 1))
	            {
	                if (BanCo.ViTri[i][j].Trong == true) turn = true;
	                if (BanCo.ViTri[i][j].Trong == false)
	                    if (BanCo.ViTri[i][j].Phe != this.Phe) turn = true;
	            }
		}else {
			if ((i == Hang + 1 && j == Cot + 1) || (i == Hang + 1 && j == Cot - 1) || (i == Hang - 1 && j == Cot - 1) || (i == Hang - 1 && j == Cot + 1))
	        {
	            if (BanCo.ViTri[i][j].Trong == true) turn = true;
	            if (BanCo.ViTri[i][j].Trong == false)
	                if (BanCo.ViTri[i][j].Phe != this.Phe) turn = true;
	        }
		}
        // Kiểm tra lộ mặt tướng
        if(VanCo.state)
        {
        		if (VanCo.n1.qTuong.Cot == VanCo.n2.qTuong.Cot && Cot == VanCo.n2.qTuong.Cot)
                {
                    int ct = 0;
                    if (j != Cot)
                    {
                        VanCo.OCoTrong(Hang, Cot);
                        for (int t = VanCo.n1.qTuong.Hang + 1; t < VanCo.n2.qTuong.Hang; t++)
                        {
                            if (BanCo.ViTri[t][Cot].Trong == false) ct++;
                        }
                        if (ct == 0) turn = false;
                        BanCo.ViTri[Hang][Cot].Trong = false;
                        BanCo.ViTri[Hang][Cot].Ten = this.Ten;
                        BanCo.ViTri[Hang][Cot].Phe = this.Phe;
                        BanCo.ViTri[Hang][Cot].ThuTu = this.ThuTu;
                    }
                }
        }else{
        		if (VanCo.n1.qTuong.Cot == VanCo.computer.qTuong.Cot && Cot == VanCo.n1.qTuong.Cot)
                {
                    int ct = 0;
                    if (j != Cot)
                    {
                        VanCo.OCoTrong(Hang, Cot);
                        for (int t = VanCo.computer.qTuong.Hang + 1; t < VanCo.n1.qTuong.Hang; t++)
                        {
                            if (BanCo.ViTri[t][Cot].Trong == false) ct++;
                        }
                        if (ct == 0) turn = false;
                        BanCo.ViTri[Hang][Cot].Trong = false;
                        BanCo.ViTri[Hang][Cot].Ten = this.Ten;
                        BanCo.ViTri[Hang][Cot].Phe = this.Phe;
                        BanCo.ViTri[Hang][Cot].ThuTu = this.ThuTu;
                    }
                }
        }
        if(turn) return 1;
        else return 0;
	}
	
	@Override
	public int TuongAnToan(int i, int j) {
		boolean turn = true;
        //Kiểm tra tướng an toàn
        if (turn == true)
        {
            int ho, co;
            QuanCo temp = null;
            temp = new QuanCo();

            ho = this.Hang;
            co = this.Cot;
            if(VanCo.state)
            {
            	if (BanCo.ViTri[i][j].Trong == false)
                {
            		if(BanCo.ViTri[i][j].Phe == VanCo.n1.Phe){
            			for(int k = 0; k < VanCo.n1.dsQuanCo.size(); k++)
                		{
                			if(VanCo.n1.dsQuanCo.get(k).Ten == BanCo.ViTri[i][j].Ten && VanCo.n1.dsQuanCo.get(k).ThuTu == BanCo.ViTri[i][j].ThuTu)
                				temp = VanCo.n1.dsQuanCo.get(k);
                		}	
            		}else {
            			for(int k = 0; k < VanCo.n2.dsQuanCo.size(); k++)
                		{
                			if(VanCo.n2.dsQuanCo.get(k).Ten == BanCo.ViTri[i][j].Ten && VanCo.n2.dsQuanCo.get(k).ThuTu == BanCo.ViTri[i][j].ThuTu)
                				temp = VanCo.n2.dsQuanCo.get(k);
                		}	
					}
                }
            }else{
            	if(BanCo.ViTri[i][j].Trong == false)
            	{
            		if(BanCo.ViTri[i][j].Phe == VanCo.computer.Phe){
            			for(int k = 0; k < VanCo.computer.dsQuanCo.size(); k++)
                		{
                			if(VanCo.computer.dsQuanCo.get(k).Ten == BanCo.ViTri[i][j].Ten && VanCo.computer.dsQuanCo.get(k).ThuTu == BanCo.ViTri[i][j].ThuTu)
                				temp = VanCo.computer.dsQuanCo.get(k);
                		}	
            		}else {
            			for(int k = 0; k < VanCo.n1.dsQuanCo.size(); k++)
                		{
                			if(VanCo.n1.dsQuanCo.get(k).Ten == BanCo.ViTri[i][j].Ten && VanCo.n1.dsQuanCo.get(k).ThuTu == BanCo.ViTri[i][j].ThuTu)
                				temp = VanCo.n1.dsQuanCo.get(k);
                		}	
    				}
            	}
            }
            //Giả sử quân cờ được đi để kiểm tra Tướng mình có bị chiếu ko
            VanCo.OCoTrong(Hang, Cot);
            BanCo.ViTri[i][j].Trong = false;
            BanCo.ViTri[i][j].Phe = Phe;
            BanCo.ViTri[i][j].Ten = Ten;
            BanCo.ViTri[i][j].ThuTu = ThuTu;
            this.Hang = i;
            this.Cot = j;
            if (temp.Phe != 2)
            {
                temp.TrangThai = 0;
            }
            //Kiểm tra
            if(VanCo.state)
            {
            	if(this.Phe == VanCo.n1.Phe)
            	{
            		if (VanCo.ChieuTuong(VanCo.n1.qTuong) == true) turn = false;
            	}else{
            		if (VanCo.ChieuTuong(VanCo.n2.qTuong) == true) turn = false;
            	}
            }else {
            	if(this.Phe == VanCo.n1.Phe)
            	{
            		if (VanCo.ChieuTuong(VanCo.n1.qTuong) == true) turn = false;
            	}else{
            		if (VanCo.ChieuTuong(VanCo.computer.qTuong) == true) turn = false;
            	}
			}
            //Trả lại những gì đã giả sử ở trên ^^!
            this.Hang = ho;
            this.Cot = co;
            VanCo.OCoTrong(i, j);
            BanCo.ViTri[ho][co].Trong = false;
            BanCo.ViTri[ho][co].Phe = Phe;
            BanCo.ViTri[ho][co].Ten = Ten;
            BanCo.ViTri[ho][co].ThuTu = ThuTu;
            if (temp.Phe != 2)
            {
            	if(temp.isFirst && temp.Ten != "tuong")
					temp.TrangThai = 2;
				else
					temp.TrangThai = 1;
                temp.imgUnit.setVisibility(ImageView.VISIBLE);
                BanCo.ViTri[i][j].Trong = false;
                BanCo.ViTri[i][j].Ten = temp.Ten;
                BanCo.ViTri[i][j].Phe = temp.Phe;
                BanCo.ViTri[i][j].ThuTu = temp.ThuTu;
                BanCo.ViTri[i][j].TrangThai = temp.TrangThai;
            }
        }
        //Trả về kết quả
        if (turn) return 1;
        else return 0;
    }
}
